"use client";

import { useEffect } from 'react';
import { useAuth } from './context';
import { useRouter } from 'next/navigation';

export function RequireAuth({ children }: { children: React.ReactNode }) {
  const { user, loading } = useAuth();
  const router = useRouter();

  useEffect(() => {
    if (!loading && !user) {
      router.replace('/auth/login');
    }
  }, [loading, user, router]);

  if (loading) {
    return (
      <div className="min-h-dvh flex items-center justify-center text-neutral-600">Loading...</div>
    );
  }
  if (!user) return null;
  return <>{children}</>;
}